package final_project;

public class Classification {
    protected char section;
    protected int mainClass;
    protected char subclass;
    protected int mainGroup;
    protected int subgroup;

    public final static char SEPARATOR = '/';

    public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
        this.section = section;
        this.mainClass = mainClass;
        this.subclass = subclass;
        this.mainGroup = mainGroup;
        this.subgroup = subgroup;
    }

    // example: G09F 17/30
    public Classification(String symbol) {
        this.section = symbol.charAt(0);
        this.mainClass = Integer.parseInt(symbol.substring(1, 3));
        this.subclass = symbol.charAt(3);

        int pos = symbol.indexOf(SEPARATOR);
        this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim());
        this.subgroup = Integer.parseInt(symbol.substring(pos + 1));
    }

    public char getSection() {
        return section;
    }

    public void setSection(char section) {
        this.section = section;
    }

    public int getMainClass() {
        return mainClass;
    }

    public void setMainClass(int mainClass) {
        this.mainClass = mainClass;
    }

    public char getSubclass() {
        return subclass;
    }

    public void setSubclass(char subclass) {
        this.subclass = subclass;
    }

    public int getMainGroup() {
        return mainGroup;
    }

    public void setMainGroup(int mainGroup) {
        this.mainGroup = mainGroup;
    }

    public int getSubgroup() {
        return subgroup;
    }

    public void setSubgroup(int subgroup) {
        this.subgroup = subgroup;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;

        result = prime * result + mainClass;
        result = prime * result + mainGroup;
        result = prime * result + section;
        result = prime * result + subclass;
        result = prime * result + subgroup;

        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }

        Classification other = (Classification) obj;
        if (mainClass != other.mainClass || mainGroup != other.mainGroup ||
                section != other.section || subclass != other.subclass || subgroup != other.subgroup) {
            return false;
        }

        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(section);

        String strMainClass = String.format("%2s", mainClass).replace(' ', '0');
        sb.append(strMainClass);

        sb.append(subclass);

        String strMainGroup = String.format("%4s", mainGroup);
        sb.append(strMainGroup);

        sb.append(SEPARATOR);

        sb.append(subgroup);

        return sb.toString();
    }
}
